import React, { useCallback } from 'react';
import {
  HomeOutlined,
  LogoutOutlined,
  MonitorOutlined,
  SettingOutlined,
  UserOutlined,
} from '@ant-design/icons';
import { Avatar, Menu, Spin } from 'antd';
import { history, useModel } from 'umi';
import { stringify } from 'querystring';
import HeaderDropdown from '../HeaderDropdown';
import styles from './index.less';
import type { MenuInfo } from 'rc-menu/lib/interface';
import { logout } from '@/services';

const AvatarDropdown: React.FC<any> = () => {
  const { initialState, setInitialState } = useModel('@@initialState');
  // const { dispatch } = useModel('system');
  /**
   * 退出登录，并且将当前的 url 保存
   */
  const loginOut = async () => {
    await logout();
    const { query = {}, pathname } = history.location;
    const { redirect } = query;
    // Note: There may be security issues, please note
    if (window.location.pathname !== '/user/login' && !redirect) {
      history.replace({
        pathname: '/user/login',
        search: stringify({
          redirect: pathname,
        }),
      });
    }
  };

  const onMenuClick = useCallback(
    (event: MenuInfo) => {
      const { key } = event;
      if (key === 'logout') {
        setInitialState((s) => ({ ...s, currentUser: undefined, menus: undefined }));
        // dispatch({ type: 'CHANGESTATE', payload: { tabList: [] } });
        loginOut();
        return;
      }
      history.push(`${key}`);
    },
    [setInitialState],
  );

  const loading = (
    <span className={`${styles.action} ${styles.account}`}>
      <Spin
        size="small"
        style={{
          marginLeft: 8,
          marginRight: 8,
        }}
      />
    </span>
  );

  if (!initialState) {
    return loading;
  }

  const { currentUser } = initialState;
  // if (currentUser?.userName === 'anon') {
  //   setIsAnonymous(true);
  // }

  if (!currentUser || !currentUser.userName) {
    return loading;
  }

  const menuHeaderDropdown = (
    <Menu className={styles.menu} selectedKeys={[]} onClick={onMenuClick}>
      <Menu.Item key="/account/center">
        <UserOutlined />
        个人中心
      </Menu.Item>
      <Menu.Item key="/account/settings">
        <SettingOutlined />
        个人设置
      </Menu.Item>

      <Menu.Item key="/dashboard/workplace">
        <HomeOutlined />
        工作台
      </Menu.Item>

      <Menu.Item key="/dashboard/monitor">
        <MonitorOutlined />
        监控页
      </Menu.Item>

      <Menu.Item key="/dashboard/analysis">
        <MonitorOutlined />
        分析页
      </Menu.Item>

      <Menu.Divider />

      <Menu.Item key="logout">
        <LogoutOutlined />
        退出登录
      </Menu.Item>
    </Menu>
  );
  return (
    <HeaderDropdown overlay={menuHeaderDropdown} disabled={currentUser.userName === 'anon'}>
      <span className={`${styles.action} ${styles.account}`}>
        <Avatar
          size="default"
          className={styles.avatar}
          src={
            currentUser?.avatar
              ? currentUser.avatar
              : 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_bt%2F0%2F13228821048%2F641&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1671278448&t=2eca1640a1fbea59f6a1634ffd3627dc'
          }
          alt="avatar"
        />
        {/* <span className={`${styles.name} anticon`}>
          {currentUser.userName === 'anon' ? '游客' : currentUser.userName}
        </span> */}
      </span>
    </HeaderDropdown>
  );
};

export default AvatarDropdown;
